package com.view.jurisdictionmanage;

import com.bean.User;
import com.dao.UserDao;
import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoComboBox;
import com.lovo.netCRM.component.LovoTable;
import com.service.UserService;
import com.service.impl.UserServiceImpl;
import com.util.PageUtilSuper;
import com.view.MainFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class UserJurManagePanel extends JPanel {
    private MainFrame frame;

    private LovoTable userTable;

    private LovoButton addUserBtn;
    private LovoButton delUserBtn;
    private LovoButton chaUserBtn;

    private JPanel queryPanel;
    private JLabel userNameLab;
    private JTextField userNameTxt;
    private JButton queryBtn;

    private LovoButton prev,next;
    private JLabel txtPage;
    private PageUtilSuper<User> pageUtilSuper;
    private int pgNumber = 1;
    private int pageSize = 3;

    private UserService userService = new UserServiceImpl();

    public UserJurManagePanel(MainFrame frame) {
        this.frame = frame;
        this.setBackground(Color.WHITE);
        setPageUtilSuper();
        this.setLayout(null);
        this.initTable();
        this.initBtn();
        this.initQueryPanel();

        txtPage = new JLabel();
        this.txtPage.setBounds(170, 305, 100, 50);
        this.add(this.txtPage);
        labelInit();
    }

    private void labelInit() {
        int maxPage = pageUtilSuper.getMaxNumber();
        this.txtPage.setText(pgNumber+" / "+maxPage);
    }

    public void setPageUtilSuper() {
        List<User> userList = new ArrayList<>();
        userList = userService.getAllUsers();
        pageUtilSuper = new PageUtilSuper(pageSize,userList);
    }

    public void flush(){
        tableFlush();
        labelInit();
    }

    public void tableFlush(){
        setPageUtilSuper();
        List<User> userList = new ArrayList<>();
        userList =pageUtilSuper.getPgNumberThing(pgNumber) ;
        userTable.updateLovoTable(userList);
    }

    private void initTable() {
        // TODO Auto-generated method stub
        this.userTable = new LovoTable(this, new String[]{"用户名", "密码",
                "角色", "真实姓名"}, new String[]{"account", "password",
                "roleList", "realName"},// 数组中应该是对应属性的属性名
                "id");// 填入唯一标示属性
        List<User> userList = new ArrayList<>();
        userList =pageUtilSuper.getPgNumberThing(1) ;
        userTable.updateLovoTable(userList);
        this.userTable.setSizeAndLocation(0, 0, 800, 300);
    }

    private void initBtn() {
        // TODO Auto-generated method stub
        this.addUserBtn = new LovoButton("添加新用户", 30, 380, this);
        this.delUserBtn = new LovoButton("删除用户", 30, 450, this);
        this.chaUserBtn = new LovoButton("修改用户权限",250,380,this);
        this.prev = new LovoButton("上一页",30,320,this);
        this.next = new LovoButton("下一页",250,320,this);
        prev.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (pageUtilSuper.havePrev(pgNumber)) {
                    pgNumber--;
                    flush();
                } else {
                    JOptionPane.showMessageDialog(null, "当前是第一页");
                }
            }
        });

        next.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (pageUtilSuper.haveNext(pgNumber)) {
                    pgNumber ++;
                    flush();
                } else {
                    JOptionPane.showMessageDialog(null, "当前是最后一页");
                }
            }
        });

        addUserBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                UserAddDialog userAddDialog = new UserAddDialog(frame);
            }
        });

        delUserBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int key = userTable.getKey();
                if(key == -1 ){
                    JOptionPane.showMessageDialog(null,"请先选择一行");
                }else {
                    int m = JOptionPane.showConfirmDialog(null,"你确定要删除吗？");
                    if(m == 0 ){
                        if(userService.deleteUser(key)){
                            JOptionPane.showMessageDialog(null,"删除成功");
                            frame.flush();
                        }else {
                            JOptionPane.showMessageDialog(null,"删除失败");
                        }
                    }
                }
            }
        });

        chaUserBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int key = userTable.getKey();
                if(key == -1){
                    JOptionPane.showMessageDialog(null,"请先选择一行");
                }else{
                    UserUpdateDialog userUpdateDialog = new UserUpdateDialog(frame,key);
                }
            }
        });
    }

    private void initQueryPanel() {
        // TODO Auto-generated method stub
        this.queryPanel = new JPanel();
        this.queryPanel.setBorder(BorderFactory.createTitledBorder("查询用户信息"));
        this.queryPanel.setBackground(Color.WHITE);
        this.queryPanel.setLayout(null);
        this.queryPanel.setBounds(380, 310, 300, 180);
        this.add(this.queryPanel);

        this.userNameLab = new JLabel("用户名称");
        this.userNameLab.setBounds(30, 30, 50, 30);
        this.queryPanel.add(this.userNameLab);
        this.userNameTxt = new JTextField();
        this.userNameTxt.setBounds(100, 35, 130, 20);
        this.queryPanel.add(this.userNameTxt);

        this.queryBtn = new JButton("查找");
        this.queryBtn.setBounds(200, 140, 80, 20);
        this.queryPanel.add(this.queryBtn);

        queryBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String userNameTxtText = userNameTxt.getText();
                List<User> list = new ArrayList<>();
                list = userService.getFindUsers(userNameTxtText);
                userTable.updateLovoTable(list);
            }
        });

    }
}
